home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gdevpdfr.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  11.5 KB  |  426 lines

  1. /* Copyright (C) 1997, 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gdevpdfr.c,v 1.4 2000/09/19 19:00:17 lpd Exp $ */
  20. /* Named object pdfmark processing */
  21. #include "memory_.h"
  22. #include "string_.h"
  23. #include "gx.h"
  24. #include "gserrors.h"
  25. #include "gsutil.h"        /* for bytes_compare */
  26. #include "gdevpdfx.h"
  27. #include "gdevpdfo.h"
  28. #include "scanchar.h"
  29. #include "strimpl.h"
  30. #include "sstring.h"
  31.  
  32. #ifndef gs_error_syntaxerror
  33. #  define gs_error_syntaxerror gs_error_rangecheck
  34. #endif
  35.  
  36. /* Test whether an object name has valid syntax, {name}. */
  37. bool
  38. pdf_objname_is_valid(const byte *data, uint size)
  39. {
  40.     return (size >= 2 && data[0] == '{' &&
  41.         (const byte *)memchr(data, '}', size) == data + size - 1);
  42. }
  43.  
  44. /*
  45.  * Look up a named object.  Return e_rangecheck if the syntax is invalid.
  46.  * If the object is missing, return e_undefined.
  47.  */
  48. int
  49. pdf_find_named(gx_device_pdf * pdev, const gs_param_string * pname,
  50.            cos_object_t **ppco)
  51. {
  52.     const cos_value_t *pvalue;
  53.  
  54.     if (!pdf_objname_is_valid(pname->data, pname->size))
  55.     return_error(gs_error_rangecheck);
  56.     if ((pvalue = cos_dict_find(pdev->named_objects, pname->data, pname->size)) != 0) {
  57.     *ppco = pvalue->contents.object;
  58.     return 0;
  59.     }
  60.     return_error(gs_error_undefined);
  61. }
  62.  
  63. /*
  64.  * Create a named object.  id = -1L means do not assign an id.  pname = 0
  65.  * means just create the object, do not name it.
  66.  */
  67. int
  68. pdf_create_named(gx_device_pdf *pdev, const gs_param_string *pname,
  69.          cos_type_t cotype, cos_object_t **ppco, long id)
  70. {
  71.     cos_object_t *pco;
  72.     cos_value_t value;
  73.  
  74.     *ppco = pco = cos_object_alloc(pdev, "pdf_create_named");
  75.     if (pco == 0)
  76.     return_error(gs_error_VMerror);
  77.     pco->id =
  78.     (id == -1 ? 0L : id == 0 ? pdf_obj_ref(pdev) : id);
  79.     if (pname) {
  80.     int code = cos_dict_put(pdev->named_objects, pname->data,
  81.                 pname->size, cos_object_value(&value, pco));
  82.  
  83.     if (code < 0)
  84.         return code;
  85.     }
  86.     if (cotype != cos_type_generic)
  87.     cos_become(pco, cotype);
  88.     *ppco = pco;
  89.     return 0;
  90. }
  91. int
  92. pdf_create_named_dict(gx_device_pdf *pdev, const gs_param_string *pname,
  93.               cos_dict_t **ppcd, long id)
  94. {
  95.     cos_object_t *pco;
  96.     int code = pdf_create_named(pdev, pname, cos_type_dict, &pco, id);
  97.  
  98.     *ppcd = (cos_dict_t *)pco;
  99.     return code;
  100. }
  101.  
  102. /*
  103.  * Look up a named object as for pdf_find_named.  If the object does not
  104.  * exist, create it (as a dictionary if it is one of the predefined names
  105.  * {ThisPage}, {NextPage}, {PrevPage}, or {Page<#>}, otherwise as a
  106.  * generic object) and return 1.
  107.  */
  108. int
  109. pdf_refer_named(gx_device_pdf * pdev, const gs_param_string * pname_orig,
  110.         cos_object_t **ppco)
  111. {
  112.     const gs_param_string *pname = pname_orig;
  113.     int code = pdf_find_named(pdev, pname, ppco);
  114.     char page_name_chars[6 + 10 + 2]; /* {Page<n>}, enough for an int */
  115.     gs_param_string pnstr;
  116.     int page_number;
  117.  
  118.     if (code != gs_error_undefined)
  119.     return code;
  120.     /*
  121.      * Check for a predefined name.  Map ThisPage, PrevPage, and NextPage
  122.      * to the appropriate Page<#> name.
  123.      */
  124.     if (pname->size >= 7 &&
  125.     sscanf((const char *)pname->data, "{Page%d}", &page_number) == 1
  126.     )
  127.     goto cpage;
  128.     if (pdf_key_eq(pname, "{ThisPage}"))
  129.     page_number = pdev->next_page + 1;
  130.     else if (pdf_key_eq(pname, "{NextPage}"))
  131.     page_number = pdev->next_page + 2;
  132.     else if (pdf_key_eq(pname, "{PrevPage}"))
  133.     page_number = pdev->next_page;
  134.     else {
  135.     code = pdf_create_named(pdev, pname, cos_type_generic, ppco, 0L);
  136.     return (code < 0 ? code : 1);
  137.     }
  138.     if (page_number <= 0)
  139.     return code;
  140.     sprintf(page_name_chars, "{Page%d}", page_number);
  141.     param_string_from_string(pnstr, page_name_chars);
  142.     pname = &pnstr;
  143.     code = pdf_find_named(pdev, pname, ppco);
  144.     if (code != gs_error_undefined)
  145.     return code;
  146.  cpage:
  147.     if (pdf_page_id(pdev, page_number) <= 0)
  148.     return_error(gs_error_rangecheck);
  149.     *ppco = COS_OBJECT(pdev->pages[page_number - 1].Page);
  150.     return 0;
  151. }
  152.  
  153. /*
  154.  * Look up a named object as for pdf_refer_named.  If the object already
  155.  * exists and is not simply a forward reference, return e_rangecheck;
  156.  * if it exists as a forward reference, set its type and return 0;
  157.  * otherwise, create the object with the given type and return 1.
  158.  */
  159. int
  160. pdf_make_named(gx_device_pdf * pdev, const gs_param_string * pname,
  161.            cos_type_t cotype, cos_object_t **ppco, bool assign_id)
  162. {
  163.     if (pname) {
  164.     int code = pdf_refer_named(pdev, pname, ppco);
  165.     cos_object_t *pco = *ppco;
  166.  
  167.     if (code < 0)
  168.         return code;
  169.     if (cos_type(pco) != cos_type_generic)
  170.         return_error(gs_error_rangecheck);
  171.     if (assign_id && pco->id == 0)
  172.         pco->id = pdf_obj_ref(pdev);
  173.     cos_become(pco, cotype);
  174.     return code;
  175.     } else {
  176.     int code = pdf_create_named(pdev, pname, cotype, ppco,
  177.                     (assign_id ? 0L : -1L));
  178.  
  179.     return (code < 0 ? code : 1);
  180.     }
  181. }
  182. int
  183. pdf_make_named_dict(gx_device_pdf * pdev, const gs_param_string * pname,
  184.             cos_dict_t **ppcd, bool assign_id)
  185. {
  186.     cos_object_t *pco;
  187.     int code = pdf_make_named(pdev, pname, cos_type_dict, &pco, assign_id);
  188.  
  189.     *ppcd = (cos_dict_t *)pco;
  190.     return code;
  191. }
  192.  
  193. /*
  194.  * Look up a named object as for pdf_refer_named.  If the object does not
  195.  * exist, return e_undefined; if the object exists but has the wrong type,
  196.  * return e_typecheck.
  197.  */
  198. int
  199. pdf_get_named(gx_device_pdf * pdev, const gs_param_string * pname,
  200.           cos_type_t cotype, cos_object_t **ppco)
  201. {
  202.     int code = pdf_refer_named(pdev, pname, ppco);
  203.  
  204.     if (code < 0)
  205.     return code;
  206.     if (cos_type(*ppco) != cotype)
  207.     return_error(gs_error_typecheck);
  208.     return code;
  209. }
  210.  
  211. /*
  212.  * Scan a token from a string.  <<, >>, [, and ] are treated as tokens.
  213.  * Return 1 if a token was scanned, 0 if we reached the end of the string,
  214.  * or an error.  On a successful return, the token extends from *ptoken up
  215.  * to but not including *pscan.
  216.  *
  217.  * Note that this scanner expects a subset of PostScript syntax, not PDF
  218.  * syntax.  In particular, it doesn't understand ASCII85 strings, and it
  219.  * doesn't process the PDF #-escape syntax within names.  Note also that
  220.  * it does only minimal syntax checking.
  221.  */
  222. int
  223. pdf_scan_token(const byte **pscan, const byte * end, const byte **ptoken)
  224. {
  225.     const byte *p = *pscan;
  226.  
  227.     while (p < end && scan_char_decoder[*p] == ctype_space)
  228.     ++p;
  229.     *ptoken = p;
  230.     if (p >= end) {
  231.     *pscan = p;
  232.     return 0;
  233.     }
  234.     switch (*p) {
  235.     case '%':
  236.     case ')':
  237.     return_error(gs_error_syntaxerror);
  238.     case '(': {
  239.     /* Skip over the string. */
  240.     byte buf[50];        /* size is arbitrary */
  241.     stream_cursor_read r;
  242.     stream_cursor_write w;
  243.     stream_PSSD_state ss;
  244.     int status;
  245.  
  246.     s_PSSD_init_inline(&ss);
  247.     r.ptr = p;        /* skip the '(' */
  248.     r.limit = end - 1;
  249.     w.limit = buf + sizeof(buf) - 1;
  250.     do {
  251.         /* One picky compiler complains if we initialize to buf - 1. */
  252.         w.ptr = buf;  w.ptr--;
  253.         status = (*s_PSSD_template.process)
  254.         ((stream_state *) & ss, &r, &w, true);
  255.     }
  256.     while (status == 1);
  257.     *pscan = r.ptr + 1;
  258.     return 1;
  259.     }
  260.     case '<':
  261.     if (end - p < 2)
  262.         return_error(gs_error_syntaxerror);
  263.     if (p[1] != '<') {
  264.         /*
  265.          * We need the cast because some compilers declare memchar as
  266.          * returning a char * rather than a void *.
  267.          */
  268.         p = (const byte *)memchr(p + 1, '>', end - p - 1);
  269.         if (p == 0)
  270.         return_error(gs_error_syntaxerror);
  271.     }
  272.     goto m2;
  273.     case '>':
  274.     if (end - p < 2 || p[1] != '>')
  275.         return_error(gs_error_syntaxerror);
  276. m2:    *pscan = p + 2;
  277.     return 1;
  278.     case '[': case ']': case '{': case '}':
  279.     *pscan = p + 1;
  280.     return 1;
  281.     case '/':
  282.     ++p;
  283.     default:
  284.     break;
  285.     }
  286.     while (p < end && scan_char_decoder[*p] <= ctype_name)
  287.     ++p;
  288.     *pscan = p;
  289.     if (p == *ptoken)        /* no chars scanned, i.e., not ctype_name */
  290.     return_error(gs_error_syntaxerror);
  291.     return 1;
  292. }
  293. /*
  294.  * Scan a possibly composite token: arrays and dictionaries are treated as
  295.  * single tokens.
  296.  */
  297. int
  298. pdf_scan_token_composite(const byte **pscan, const byte * end,
  299.              const byte **ptoken_orig)
  300. {
  301.     int level = 0;
  302.     const byte *ignore_token;
  303.     const byte **ptoken = ptoken_orig;
  304.     int code;
  305.  
  306.     do {
  307.     code = pdf_scan_token(pscan, end, ptoken);
  308.     if (code <= 0)
  309.         return (code < 0 || level == 0 ? code :
  310.             gs_note_error(gs_error_syntaxerror));
  311.     switch (**ptoken) {
  312.     case '<': case '[': case '{':
  313.         ++level; break;
  314.     case '>': case ']': case '}':
  315.         if (level == 0)
  316.         return_error(gs_error_syntaxerror);
  317.         --level; break;
  318.     }
  319.     ptoken = &ignore_token;
  320.     } while (level);
  321.     return code;
  322. }
  323.  
  324. /* Replace object names with object references in a (parameter) string. */
  325. private const byte *
  326. pdfmark_next_object(const byte * scan, const byte * end, const byte **pname,
  327.             cos_object_t **ppco, gx_device_pdf * pdev)
  328. {
  329.     /*
  330.      * Starting at scan, find the next object reference, set *pname
  331.      * to point to it in the string, store the object at *ppco,
  332.      * and return a pointer to the first character beyond the
  333.      * reference.  If there are no more object references, set
  334.      * *pname = end, *ppco = 0, and return end.
  335.      */
  336.     int code;
  337.  
  338.     while ((code = pdf_scan_token(&scan, end, pname)) != 0) {
  339.     gs_param_string sname;
  340.  
  341.     if (code < 0) {
  342.         ++scan;
  343.         continue;
  344.     }
  345.     if (**pname != '{')
  346.         continue;
  347.     /* Back up over the { and rescan as a single token. */
  348.     scan = *pname;
  349.     code = pdf_scan_token_composite(&scan, end, pname);
  350.     if (code < 0) {
  351.         ++scan;
  352.         continue;
  353.     }
  354.     sname.data = *pname;
  355.     sname.size = scan - sname.data;
  356.     /*
  357.      * Forward references are allowed.  If there is an error,
  358.      * simply retain the name as a literal string.
  359.      */
  360.     code = pdf_refer_named(pdev, &sname, ppco);
  361.     if (code < 0)
  362.         continue;
  363.     return scan;
  364.     }
  365.     *ppco = 0;
  366.     return end;
  367. }
  368. int
  369. pdf_replace_names(gx_device_pdf * pdev, const gs_param_string * from,
  370.           gs_param_string * to)
  371. {
  372.     const byte *start = from->data;
  373.     const byte *end = start + from->size;
  374.     const byte *scan;
  375.     uint size = 0;
  376.     cos_object_t *pco;
  377.     bool any = false;
  378.     byte *sto;
  379.     char ref[1 + 10 + 5 + 1];    /* max obj number is 10 digits */
  380.  
  381.     /* Do a first pass to compute the length of the result. */
  382.     for (scan = start; scan < end;) {
  383.     const byte *sname;
  384.     const byte *next =
  385.         pdfmark_next_object(scan, end, &sname, &pco, pdev);
  386.  
  387.     size += sname - scan;
  388.     if (pco) {
  389.         sprintf(ref, " %ld 0 R ", pco->id);
  390.         size += strlen(ref);
  391.     }
  392.     scan = next;
  393.     any |= next != sname;
  394.     }
  395.     to->persistent = true;    /* ??? */
  396.     if (!any) {
  397.     to->data = start;
  398.     to->size = size;
  399.     return 0;
  400.     }
  401.     sto = gs_alloc_bytes(pdev->pdf_memory, size, "pdf_replace_names");
  402.     if (sto == 0)
  403.     return_error(gs_error_VMerror);
  404.     to->data = sto;
  405.     to->size = size;
  406.     /* Do a second pass to do the actual substitutions. */
  407.     for (scan = start; scan < end;) {
  408.     const byte *sname;
  409.     const byte *next =
  410.         pdfmark_next_object(scan, end, &sname, &pco, pdev);
  411.     uint copy = sname - scan;
  412.     int rlen;
  413.  
  414.     memcpy(sto, scan, copy);
  415.     sto += copy;
  416.     if (pco) {
  417.         sprintf(ref, " %ld 0 R ", pco->id);
  418.         rlen = strlen(ref);
  419.         memcpy(sto, ref, rlen);
  420.         sto += rlen;
  421.     }
  422.     scan = next;
  423.     }
  424.     return 0;
  425. }
  426.